<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>选课列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/easyui/css/demo.css}">
    <script type="text/javascript" th:src="@{/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
    <script type="text/javascript" th:src="@{/easyui/js/validateExtends.js}"></script>
    <script type="text/javascript" th:inline="javascript">
        $(function() {
            $('#dataList').datagrid({
                title:'考勤列表',
                iconCls:'icon-more',
                border: true,
                collapsible: false,
                fit: true,
                method: "post",
                url: "getAttendanceList",
                idField:'id',
                singleSelect: true,
                pagination: true,
                rownumbers: true,
                sortName:'id',
                sortOrder:'DESC',
                remoteSort: false,
                columns: [
                [
                    {field:'chk',checkbox: true,width:50},
                    {field:'id',title:'ID',width:50, sortable: true},
                    {field:'studentId',title:'学生',width:200,
                        formatter: function(value,row,index){
                            if (row.studentId){
                                var studentList = $("#studentList").combobox("getData");
                                for(var i=0;i<studentList.length;i++ ){
                                    if(row.studentId === studentList[i].id) {
                                        return studentList[i].username;
                                    }
                                }
                                return row.studentId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'courseId',title:'课程',width:200,
                        formatter: function(value,row,index){
                            if (row.courseId){
                                var courseList = $("#courseList").combobox("getData");
                                for(var i=0;i<courseList.length;i++ ){
                                    if(row.courseId === courseList[i].id) {
                                        return courseList[i].name;
                                    }
                                }
                                return row.courseId;
                            } else {
                                return 'not found';
                            }
                        }
                    },
                    {field:'type',title:'签到类型',width:200, sortable: false},
                    {field:'date',title:'签到日期',width:200, sortable: false}
                ]
                ],
                toolbar: "#toolbar",
                onBeforeLoad : function(){
                    preLoadClazz();
                    $("#studentList").combobox("getData");
                    $("#courseList").combobox("getData");
                }
            });
            // 提前加载学生和课程信息
            function preLoadClazz(){
                $("#studentList").combobox({
                    width: "80",
                    height: "25",
                    valueField: "id",
                    textField: "username",
                    multiple: false, //可多选
                    editable: false, //不可编辑
                    method: "post",
                    url: "/student/getStudentList?from=combox",

                });
                $("#courseList").combobox({
                    width: "80",
                    height: "25",
                    valueField: "id",
                    textField: "name",
                    multiple: false,
                    editable: false,
                    method: "post",
                    url: "/course/getCourseList?from=combox",
                });
            }

            // 设置分页控件
            var p = $('#dataList').datagrid('getPager');
            $(p).pagination({
                pageSize: 10,
                pageList: [10,20,30,50,100],
                beforePageText: '第',
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });

            // 设置工具类按钮
            $("#add").click(function(){
                $("#addDialog").dialog("open");
            });

            // 补签
            $("#edit").click(function(){
                var selectRow = $("#dataList").datagrid("getSelected");
                console.log(selectRow);
                if(selectRow == null){
                    $.messager.alert("消息提醒", "请选择数据进行补签!", "warning");
                } else{
                    var id = selectRow.id;
                    $.messager.confirm("消息提醒", "若签到类型为补签，可能会出现错误，确认继续？", function(r){
                        if(r){
                            $.ajax({
                                type: "post",
                                url: "reissue",
                                data: {id: id},
                                success: function(data){
                                    if(data.success){
                                        $.messager.alert("消息提醒",data.message,"info");
                                        // 刷新表格
                                        $("#dataList").datagrid("reload");
                                    } else{
                                        $.messager.alert("消息提醒",data.message,"warning");
                                    }
                                }
                        });
                        }
                    });
                }
            });

            // 设置添加窗口
            $("#addDialog").dialog({
                title: "添加考勤",
                width: 450,
                height: 300,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                buttons: [
                    {
                        text:'添加',
                        plain: true,
                        iconCls:'icon-book-add',
                        handler:function(){
                            var validate = $("#addForm").form("validate");
                            if(!validate){
                                $.messager.alert("消息提醒","请检查你输入的数据!","warning");
                            } else{
                                $.ajax({
                                    type: "post",
                                    url: "addAttendance",
                                data: $("#addForm").serialize(),
                                    success: function(data){
                                    if(data.success){
                                        $.messager.alert("消息提醒",data.message,"info");
                                        // 关闭窗口
                                        $("#addDialog").dialog("close");

                                        // 清空原表格数据
                                        $("#add_name").textbox('setValue', "");

                                        // 刷新
                                        $('#dataList').datagrid("reload");
                                    }else{
                                        $.messager.alert("消息提醒",data.message,"warning");
                                    }
                                }
                            });
                            }
                        }
                    },
                    {
                        text:'重置',
                        plain: true,
                        iconCls:'icon-book-reset',
                        handler:function(){
                            $("#add_name").textbox('setValue', "");
                        }
                    },
                ]
            });

            // 下拉框通用属性
            $("#add_courseList,#courseList,#add_typeList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false,
                editable: false,
                method: "post",
            });

            $("#add_studentList,#studentList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "username",
                multiple: false, //不可多选
                editable: false, //不可编辑
                method: "post",
            });

            // 添加学生选择框
            $("#add_studentList").combobox({
                url: "/student/getStudentList?from=combox",
                onLoadSuccess: function(){
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                    getStudentSelectedCourseList(data[0].id);
                },
                onChange:function(id,o){
                    getStudentSelectedCourseList(id);
                }
            });

            // 添加课程选择框
            $("#add_courseList").combobox({
                url: "/course/getCourseList?from=combox",
                onLoadSuccess: function(){
                    // 默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }

            });

            // 学生选课列表
            function getStudentSelectedCourseList(sid){
                // 添加课程选择框
                console.log("sid:", sid);
                var u = "getStudentSelectedCourseList";
                $("#add_courseList").combobox({
                    url:u+"?studentid="+sid,
                    onLoadSuccess: function(){
                        // 默认选择第一条数据
                        var data = $(this).combobox("getData");
                        console.log("getData:", data);
                        $(this).combobox("setValue", data[0].id);
                    }
                });
            }

            //搜索按钮监听事件
            $("#search-btn").click(function(){
                $('#dataList').datagrid('load',{
                    studentid: $("#studentList").combobox('getValue') === '' ? 0 : $("#studentList").combobox('getValue'),
                    courseid: $("#courseList").combobox('getValue') === '' ? 0 : $("#courseList").combobox('getValue'),
                    type: $("#typeList").combobox('getValue') === '' ? '' : $("#typeList").combobox('getValue'),
                    date:$("#date").datebox('getValue')
                });
            });
        });
    </script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0"></table>

<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">签到</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div th:if="${session.usertype == '1' || session.usertype == '3'}" style="float: left; margin-right: 10px;">
        <a id="edit" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">补签</a>
    </div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="margin-top: 3px;">
        学生：<input id="studentList" class="easyui-textbox" name="studentList" />
        课程：<input id="courseList" class="easyui-textbox" name="courseList" />
        类型：<input id="typeList" class="easyui-textbox" name="typeList" />
        时间：<input id="date" class="easyui-datebox" name="date" />
        <a id="search-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
        <a id="clear-btn" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">清空搜索</a>
    </div>
</div>

<!-- 添加数据窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8" >
            <tr>
                <td style="width:40px">学生:</td>
                <td colspan="3">
                    <input id="add_studentList" style="width: 200px; height: 30px;" class="easyui-textbox" name="studentId" data-options="required:true, missingMessage:'请选择学生'" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:40px">课程:</td>
                <td colspan="3">
                    <input id="add_courseList" style="width: 200px; height: 30px;" class="easyui-textbox" name="courseId" data-options="required:true, missingMessage:'请选择课程'" />
                </td>
                <td style="width:80px"></td>
            </tr>
            <tr>
                <td style="width:80px"></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>